﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using CXMLStudioIDE;
using CXMLStudioIDE.Common;
using System.Data;

namespace CXMLStudioIDE.Common.Entities
{
    public class ProjectCollection : CollectionBase
    {
        #region Methods

        public Project this[int index]
        {
            get
            {
                return ((Project)List[index]);
            }
            set
            {
                List[index] = value;
            }
        }

        public int Add(Project value)
        {
            return (List.Add(value));
        }

        public int IndexOf(Project value)
        {
            return (List.IndexOf(value));
        }

        public void Insert(int index, Project value)
        {
            List.Insert(index, value);
        }

        public void Remove(Project value)
        {
            List.Remove(value);
        }

        public ArrayList GetList()
        {
            return InnerList;
        }

        public bool hasElements()
        {
            return (InnerList.Count > 0 ? true : false);
        }

        public Project GetProjectByTreeviewID(int i_TreeViewID)
        {
            foreach (Project p in this)
            {
                if (p.TreeViewID == i_TreeViewID)
                    return p;
            }
            return null;
        }

        //Creates a DataTable from the projects array.
        //The project business entity are added as project children
        public DataTable GetProjectsDataTable(Solution m_solution)
        {
            bool hasSolution = (m_solution == null ? false : true);

            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("title", typeof(string));
            table.Columns.Add("parentID", typeof(string));
            int id = (hasSolution ? 2 : 1);
            int parent_id = (hasSolution ? 1 : 0);
            int business_entity_id = 0;
            int project_id = 0;

            if (hasSolution)
            {
                table.Rows.Add(1, "Solution '" + m_solution.Name + "'", 0);
            }

            this.Sort();

            //Here we add the rows. If there is a solution then we add a solution root element
            for (int i = 0; i < this.Count; i++)
            {
                //TODO: Add logic to handle multiple business entities
                Project p = (Project)this[i];
                //table.Rows.Add(id, "Project '" + this.Name + "' Scope:[" + this.Scope.Name + "] Context:[" + this.Context.Name + "]", parent_id);
                table.Rows.Add(id, "Project '" + p.Name + "' Scope:[" + p.Scope.Name + "] Context:[" + p.Context.Name + "]", parent_id);
                p.TreeViewID = id;
                project_id = id;
                table.Rows.Add(++id, "Business Entities", id - 1);
                business_entity_id = id;

                this[i].Entities.Sort();

                for (int j = 0; j < this[i].Entities.Count; j++)
                {
                    table.Rows.Add(++id, this[i].Entities[j].Name + ". Version:[" + this[i].Entities[j].EntityVersion.FullVersion + "]", business_entity_id);
                    this[i].Entities[j].TreeViewId = id;
                }
                ++id;
                ++project_id;
            }
            return table;
        }

        public Project GetProjectByID(int ID)
        {
            foreach (Project p in this)
            {
                if (p.ID == ID)
                {
                    return p;
                }
            }
            return null;
        }

        public static Project GetProjectContainingBusinessEntityByTreeViewID(ProjectCollection pec,int _BETreeviewID)
        {
            foreach(Project p in pec)
            {
                foreach(BusinessEntity b in p.Entities)
                {
                    if (b.TreeViewId == _BETreeviewID)
                        return p;
                }
            }
            return null;
        }

        public void Sort()
        {
            IComparer NameSorter = new SortProjectByNameClass();
            InnerList.Sort(NameSorter);
        }

        #endregion

    }
    public class SortProjectByNameClass : IComparer
    {
        public int Compare(object obj1, object obj2)
        {
            Project p1 = (Project)obj1;
            Project p2 = (Project)obj2;

            return (String.Compare(p1.Name, p2.Name));
        }
    }
}
